<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8"/>
        <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"/>
        <title>AJ Utils-AES/DES 加密解密</title>
        <meta name="description" content="Small Java utilities with many useful functions.  AesCrypto" />
        <meta name="keywords" content="AJ Utils, ajaxjs, ajaxjs framework, java utilities, tools, helper, AES/DES 加密解密" />
        <meta name="viewport" content="width=device-width, initial-scale=1"/>
        <link rel="preconnect" href="https://fonts.googleapis.com" />
        <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin />
        <link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@200..900&family=Noto+Serif:ital,wght@0,100..900;1,100..900&display=swap&family=Noto+Sans+SC:wght@100..900&display=swap" />
        <link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Noto+Sans+SC:wght@100..900&family=Noto+Serif:ital,wght@0,100..900;1,100..900&display=swap" /> 
        <link rel="stylesheet" href="https://framework.ajaxjs.com/static/new-ui/css/common.css" />
        <link rel="stylesheet" href="https://iam.ajaxjs.com/asset/main.css"/>
        <link rel="icon" type="image/x-icon" href="https://framework.ajaxjs.com/aj-logo/logo.ico"/>
        <script src="https://framework.ajaxjs.com/static/aj-docs/common.js"></script>
        <script>
            var _hmt = _hmt || [];
            (function() {
              var hm = document.createElement("script");
              hm.src = "https://hm.baidu.com/hm.js?208c5aea11c52991bd1c3283e62ad0ce";
              var s = document.getElementsByTagName("script")[0];
              s.parentNode.insertBefore(hm, s);
            })();
        </script>
    </head>
    <body>
        <nav>
            <div>
                <div class="links">
                    <a href="/cn">🏠 首页</a>
                    | ⚙️ 源码:
                    <a target="_blank" href="https://github.com/lightweight-component/aj-util">Github</a>/<a target="_blank" href="https://gitcode.com/lightweight-component/aj-util">Gitcode</a>
                  
                    | 🚀<a href="">英语版本</a>
                </div>
                <h1>
                    <img src="https://framework.ajaxjs.com/aj-logo/logo.png" style="vertical-align: middle;height: 45px;margin-bottom: 6px;" />
                    AJ Utils
                </h1>
                <h3>小型、干净、简单的 Java 工具库
                </h3>
            </div>
        </nav>
        <div>
            <menu>
                
                <ul>
                    <li class="selected">
                        <a href="/cn">首页</a>
                    </li>
                </ul>
                <h3>Common 常用模块</h3>
                <ul>
                    <li><a href="/common/JsonUtil-cn/">JsonUtil</a></li>
                    <li><a href="/common/Base64Helper-cn/">Base64Helper</a></li>
                    <li><a href="/common/ConvertBasicValue-cn/">ConvertBasicValue</a></li>
                    <li><a href="/common/MessageDigestHelper-cn/">HashHelper</a></li>
                    <li><a href="/common/MapTool-cn/">MapTool</a></li>
                    <li><a href="/common/ObjectHelper-cn/">ObjectHelper</a></li>
                    <li><a href="/common/RandomTools-cn/">RandomTools</a></li>
                    <li><a href="/common/RegExpUtils-cn/">RegExpUtils</a></li>
                    <li><a href="/common/StrUtil-cn/">StrUtil</a></li>
                    <li><a href="/common/BoxLogger-cn/">BoxLogger</a></li>
                    <li><a href="/common/BytesHelper-cn/">BytesHelper</a></li>
                    <li><a href="/common/EncodeTools-cn/">UrlEncode</a></li>
                    <li><a href="/common/XmlHelper-cn/">XmlHelper</a></li>
                    <li><a href="/common/WebUtils-cn/">WebUtils</a></li>
                </ul>
                <h3>Date 日期处理</h3>
                <ul>
                    <li><a href="/date/intro-cn/">简介</a></li>
                    <li><a href="/date/convert-cn/">万能日期类型转换</a></li>
                    <li><a href="/date/formatter-cn/">日期格式化</a></li>
                </ul>
                <h3>Reflection 反射</h3>
                <ul>
                    <li><a href="/reflect/Methods-cn/">Methods</a></li>
                    <li><a href="/reflect/Types-cn/">Types</a></li>
                    <li><a href="/reflect/Clazz-cn/">Clazz</a></li>
                </ul>

                <h3>Input/Output 输入/输出</h3>
                <ul>
                    <li><a href="/io/FileHelper-cn/">FileHelper</a></li>
                    <li><a href="/io/Resources-cn/">Resources</a></li>
                    <li><a href="/io/StreamHelper-cn/">DataReader/DataWriter</a></li>
                    <li><a href="/io/ZipHelper-cn/">ZipHelper</a></li>
                </ul>

                <h3>HTTP 请求</h3>
                <ul>
                    <li><a href="/http_request/Get-cn">HTTP 请求</a></li>
                     <li><a href="/http_request/advanced-usage-cn/">高级用法</a></li>
                    <li><a href="/http_request/Base-cn/">Base</a></li>
                </ul>

                <h3>加密/解密</h3>
                <ul>
                    <li><a href="/cryptography/intro-cn/">简介</a></li>
                    <li><a href="/cryptography/flow-cn/">基本流程</a></li>
                    <li><a href="/cryptography/AesCrypto-cn/">AES/DES 加密解密</a></li>
                    <li><a href="/cryptography/RsaCrypto-cn/">RSA 加密解密</a></li>
                </ul>
            </menu>
            <article class="aj-text chinese">
                <h1>AES/DES 加密解密</h1>
<h2>AES</h2>
<p>对称加密多基于<code>javax.crypto</code>包进行封装，封装在类<code>com.ajaxjs.util.cryptography</code>中。先看看 AES 的，</p>
<pre><code class="language-java">final String key = &quot;abc&quot;;
final String word = &quot;123&quot;;

@Test
void testAES() {
    String encWord = Cryptography.AES_encode(word, key);
    assertEquals(word, Cryptography.AES_decode(encWord, key));
}
</code></pre>
<p>咦~怎么还是静态方法？噢——对了，我们通过静态方法<code>Cryptography.AES_encode()</code>封装了一层，其实质是：</p>
<pre><code class="language-java">public static String AES_encode(String data, String key) {
    Cryptography cryptography = new Cryptography(Constant.AES, Cipher.ENCRYPT_MODE);
    cryptography.setSecretKey(SecretKeyMgr.getSecretKey(Constant.AES, 128, SecretKeyMgr.getRandom(Constant.SECURE_RANDOM_ALGORITHM, key)));
    cryptography.setDataStr(data);

    return cryptography.doCipherAsHexStr();
}
</code></pre>
<p>要说每次实例化对象，当然比静态方法耗资源，不过在 Java 编译器优化的今天，这多出了一点的消耗可以忽略不计。</p>
<h2>DES/TripleDES</h2>
<p>其余 DES/TripleDES 如此类推，只是算法不同~</p>
<pre><code class="language-java">@Test
void testDES() {
    String encWord = Cryptography.DES_encode(word, key);
    assertEquals(word, Cryptography.DES_decode(encWord, key));
}

@SuppressWarnings(&quot;restriction&quot;)
@Test
void test3DES() {
    // 添加新安全算法,如果用 JCE 就要把它添加进去
    // 这里 addProvider 方法是增加一个新的加密算法提供者(个人理解没有找到好的答案,求补充)
//		Security.addProvider(new com.sun.crypto.provider.SunJCE());
    // byte 数组(用来生成密钥的)
    final byte[] keyBytes = {0x11, 0x22, 0x4F, 0x58, (byte) 0x88, 0x10, 0x40, 0x38, 0x28, 0x25, 0x79, 0x51, (byte) 0xCB, (byte) 0xDD, 0x55, 0x66, 0x77, 0x29, 0x74,
            (byte) 0x98, 0x30, 0x40, 0x36, (byte) 0xE2};
    String word = &quot;This is a 3DES test. 测试&quot;;

    byte[] encoded = Cryptography.tripleDES_encode(word, keyBytes);

    assertEquals(word, Cryptography.tripleDES_decode(encoded, keyBytes));
}
</code></pre>
<h2>PBE</h2>
<p>这里说说 PBE 算法。PBE 是 DES 的加强，增加一个 Salt 盐值使其更安全。</p>
<pre><code class="language-java">byte[] salt = Cryptography.initSalt();
byte[] encData = Cryptography.PBE_encode(word, key, salt);

assertEquals(word, Cryptography.PBE_decode(encData, key, salt));
</code></pre>

            </article>
        </div>

       <footer>
            AJ-Util，开源框架 <a href="https://framework.ajaxjs.com" target="_blank">AJ-Framework</a> 的一部分。联系方式：
            frank@ajaxjs.com，<a href="https://blog.csdn.net/zhangxin09" target="_blank">作者博客</a>
            <br />
            <br />
            Copyright © 2025 Frank Cheung. All rights reserved.
        </footer>
    </body>
</html>